package com.hw.one.collect.util;

import com.ctc.wstx.util.DataUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/8/4.
 */
public class DateUtil {

    /**
     *  格式化时间
     *  日期 yyyy-MM-dd
     */
    public static final String SDF_YMD = "yyyy-MM-dd";
    /**
     *  格式化时间
     *  日期 yyyy-MM
     */
    public static final String SDF_YM = "yyyy-MM";
    /**
     * 格式化时间
     * 日期+时间
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String SDF_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 全格式之 日期 yyyy-MM-dd 00:00:00
     */
    public static final String ALL_SDF_YMD = "yyyy-MM-dd 00:00:00";
    /**
     * 全格式之 日期+小时
     * yyyy-MM-dd HH:00:00
     */
    public static final String ALL_SDF_YMD_H = "yyyy-MM-dd HH:00:00";
    /**
     * 全格式之 日期+小时+分钟
     * yyyy-MM-dd HH:mm:00
     */
    public static final String ALL_SDF_YMD_HM = "yyyy-MM-dd HH:mm:00";
    public static final String ALL_SDF_YMDH = "yyyy-MM-dd HH";

    /**
     * 没有分隔符的格式：yyyyMM
     * 例如：201703
     */
    public static final String NO_SPLIT_SDF_YM = "yyyyMM";

    public static final String NO_SPLIT_SDF_YMD = "yyyyMMdd";
    public static final String NO_SPLIT_SDF_YMDH = "yyyyMMddHH";

    public static final String NO_SPLIT_SDF_YMDHmsS = "yyyyMMddHHmmssS";

    public static final String SPLIT_SDF_YMDHMS = "yyyy/MM/dd HH:mm:ss";

    /**
     * 没有分隔符的格式：yyyy
     * 例如：2017
     */
    public static final String NO_SPLIT_SDF_Y = "yyyy";

    public static SimpleDateFormat sdf = new SimpleDateFormat(SDF_YMD_HMS);

    public static  String parseDate(Date date,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }


    /**
     * 判断是否是日期的格式
     * 2017-07-31 08:32:00
     * @param timeStr
     * @return
     */
    public static boolean isDateFormat(String timeStr) {
        String regex = "\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(timeStr);
        return matcher.matches();
    }
    /**
     * 判断是否是日期的格式
     * 2017-07-31 08:32:00
     * @param timeStr
     * @return
     */
    public static boolean isDateFormat2(String timeStr) {
        String regex = "\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}.\\d{1}";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(timeStr);
        return matcher.matches();
    }


    public static boolean checkDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return true;
        }else {
            return false;
        }
    }

    public static Date parseDateByStr(String dateStr) {
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }
    public static Date parseDateByStr(String dateStr, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }
    public static boolean compare(String date1, String date2) {
        if (!isDateFormat(date1) || !isDateFormat(date2)) {
            return false;
        }

        if (parseDateByStr(date1).getTime()> parseDateByStr(date2).getTime()) {
            return true;
        }else {
            return false;
        }
    }

    public  static long timeDiffHour(String time1, String time2){

        long timeDiff;
        long nh = 60 * 60;
        DateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyyMMddHH");
//        SimpleDateFormat farmatter1 = new SimpleDateFormat("yyyymmddhh");

        LocalDateTime date1 = LocalDateTime.parse(time1, formatter);
        LocalDateTime date2 = LocalDateTime.parse(time2, formatter);
        //获取以秒为单位的时间戳，比较
        timeDiff = date2.toEpochSecond(ZoneOffset.of("+8"))-date1.toEpochSecond(ZoneOffset.of("+8"));
        long hourDiff = timeDiff / nh;

        return hourDiff;
    }
}
